package com.lyy.photovoltaic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lyy.photovoltaic.beans.Notice;
import com.lyy.photovoltaic.common.utils.MyRandom;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.NoticeMapper;
import com.lyy.photovoltaic.service.NoticeService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public ResultVO addNotice(JSONObject jsonObject) {
//        System.out.println(jsonObject);
        Notice notice = new Notice();
        notice.setNoticeId(System.currentTimeMillis() + MyRandom.getStringRandom());
        notice.setNoticeCreatetime(new Date());
        notice.setNoticeTitle(jsonObject.getString("title"));
        notice.setNoticeContent(jsonObject.getString("content"));
        notice.setImageUrl(jsonObject.getString("imageUrl"));
        notice.setNoticeStatus(jsonObject.getBoolean("status"));
        int i = noticeMapper.insert(notice);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "公告新闻插入成功", notice);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "公告新闻插入失败", null);
    }

    @Override
    public ResultVO allVisibleNotices() {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("noticeStatus", true);
        List<Notice> notices = noticeMapper.selectByExample(example);
//        List<Notice> notices = noticeMapper.selectAll();
//        System.out.println(notices);
        if (notices.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询新闻详情成功", notices);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查询新闻详情失败", null);
    }

    @Override
    public ResultVO findNoticesByPages(int pageNum, int pageSize) {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("noticeStatus", true);
//分页查询
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<Notice> notices = noticeMapper.selectByExampleAndRowBounds(example, rowBounds);
        if (!notices.isEmpty()) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "条件分页查询新闻详情成功", notices);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "条件分页查询新闻详情失败", null);
    }

    @Override
    public int VisibleNoticeCount() {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("noticeStatus", true);
        int i = noticeMapper.selectCountByExample(example);
//        System.out.println(i);
        return i;
    }

    @Override
    public ResultVO findNoticeById(String nId) {
        Notice notice = noticeMapper.selectByPrimaryKey(nId);
        if (notice != null) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询新闻详情成功", notice);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查询新闻详情失败", null);
    }

    @Override
    public ResultVO allNotice() {
        List<Notice> notices = noticeMapper.selectAll();
        if (notices != null) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询所有成功", notices);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "不存在公告信息", null);
    }

    @Override
    public ResultVO updateNotice(Notice notice) {
//        System.out.println(notice);
        int i = noticeMapper.updateByPrimaryKeySelective(notice);
        System.out.println(i);
        if (i != 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "更新公告信息成功",null);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "更新失败", null);
    }

    @Override
    public ResultVO deleteNoticeById(String nId) {
//        System.out.println(nId);
        int i = noticeMapper.deleteByPrimaryKey(nId);
        if (i != 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "删除公告信息成功",null);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "删除失败", null);
    }

}
